package com.tduck.cloud.project.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tduck.cloud.project.entity.UserProjectItemEntity;
import com.tduck.cloud.project.entity.UserProjectResultEntity;
import com.tduck.cloud.project.entity.enums.ProjectItemTypeEnum;
import com.tduck.cloud.project.mapper.UserProjectItemMapper;
import com.tduck.cloud.project.mapper.UserProjectResultMapper;
import com.tduck.cloud.project.service.UserProjectItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 项目表单项(ProjectItem)表服务实现类
 *
 * @author smalljop
 * @since 2020-11-19 10:49:18
 */
@Service("userProjectItemService")
public class UserProjectItemServiceImpl extends ServiceImpl<UserProjectItemMapper, UserProjectItemEntity> implements UserProjectItemService {

    @Autowired
    private UserProjectResultMapper userProjectResultMapper;

    @Override
    public List<UserProjectItemEntity> listByProjectKey(String key) {
        return this.list(Wrappers.<UserProjectItemEntity>lambdaQuery()
                .eq(UserProjectItemEntity::getProjectKey, key).orderByAsc(UserProjectItemEntity::getSort));
    }

    //根据调查问卷项目key、社区id或者入户id，获取对应的调查问卷数据项（数据项包含上次填写的默认值）
    @Override
    public List<UserProjectItemEntity> transforProjuectItem(String projectKey, String communityId, String houseInvestigateId) {
        //获取对应调查问卷的所有数据项
        List<UserProjectItemEntity> projectItemList = this.listByProjectKey(projectKey);

        //社区调查问卷和入户调查问卷历史数据的处理
        List<UserProjectResultEntity> newestProjectResult = null;
        if (StringUtils.isNotEmpty(communityId) && StringUtils.isEmpty(houseInvestigateId)) {
            //社区调查问卷
            //根据调查问卷ID 和 社区ID获取最新填报记录
            newestProjectResult = userProjectResultMapper.selectList(Wrappers.<UserProjectResultEntity>lambdaQuery().eq(UserProjectResultEntity::getProjectKey, projectKey).eq(UserProjectResultEntity::getCommunityId, communityId).orderByDesc(UserProjectResultEntity::getCreateTime));
        }
        if (StringUtils.isNotEmpty(houseInvestigateId)){
            //入户调查问卷
            //根据调查问卷ID 和 入户调查ID获取最新填报记录
           newestProjectResult = userProjectResultMapper.selectList(Wrappers.<UserProjectResultEntity>lambdaQuery().eq(UserProjectResultEntity::getProjectKey,projectKey).eq(UserProjectResultEntity::getHouseInvestigateId,houseInvestigateId).orderByDesc(UserProjectResultEntity::getCreateTime));
        }
        if (newestProjectResult == null || newestProjectResult.isEmpty()) {
            return projectItemList;
        }
        UserProjectResultEntity userProjectResultEntity = newestProjectResult.get(0);
        //原数据
        Map<String, Object> originalData = userProjectResultEntity.getOriginalData();
        //过程数据(获取选择其他时，输入项的数据)
        Map<String, Object> processData = userProjectResultEntity.getProcessData();
        //遍历数据赋予默认值
        projectItemList.forEach(o-> {
            o.getDefaultValue().setValue(originalData.get("field"+o.getFormItemId()));
            //单项选择或者多项选择时，选择其他项，输入时文本的处理
            if(ProjectItemTypeEnum.RADIO.getValue().equals(o.getType().getValue())){
                //单选框处理
                if ("0".equals(originalData.get("field"+o.getFormItemId()).toString())){
                    o.setSelectedValue(processData.get("field"+o.getFormItemId()));
                }
            } else if (ProjectItemTypeEnum.CHECKBOX.getValue().equals(o.getType().getValue())) {
                //多选框处理
                if (originalData.get("field"+o.getFormItemId()).toString().contains("0")){
                    o.setSelectedValue(processData.get("field"+o.getFormItemId()));
                }
            }
        });
        return projectItemList;
    }
}